package com.jsj.service;

import com.jsj.dao.TeacherDao;
import com.jsj.domain.DTO;
import com.jsj.domain.SimpleUser;
import com.jsj.domain.Teacher;
import com.jsj.domain.uid_score;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

@Service
public class ScoureService {
    @Autowired
    TeacherDao teacherDao;
    public Hashtable<String, DTO> getScoreList(List<SimpleUser> list){
        Hashtable<String,DTO> hashtable=new Hashtable<>();
        for (SimpleUser s : list) {
            hashtable.put(s.getUserid(),new DTO(s.getName(),new ArrayList<String>()));
        }
        List<String> topics=teacherDao.getTopList("19131");
        for(int i=0;i<topics.size();i++) {
            String topic=topics.get(i);
            for(SimpleUser s:list){
                DTO ndto = hashtable.get(s.getUserid());
                ndto.getList().add("0");
            }
            System.out.println("testid:"+topic);
            List<uid_score> socrelist=teacherDao.queryProblemScore("19131",String.valueOf(topic));
            for(uid_score uidScore:socrelist){
                DTO ndto = hashtable.get(uidScore.getUserid());
                System.out.println("id:"+uidScore.getUserid()+"   socre:"+uidScore.getScore());
                List<String> list1 = ndto.getList();
                list1.set(i,uidScore.getScore());
            }
        }
        //System.out.println(hashtable.get("201905020023").getList());
        return hashtable;
    }
    public XSSFWorkbook tableToexcel()  {
        List<SimpleUser> list=teacherDao.querySimpUser("19131");
        Hashtable<String,DTO> hashtable=this.getScoreList(list);
        if(hashtable.size()>0&&!list.isEmpty()){
            int scorelen=hashtable.get(list.get(0).getUserid()).getList().size();
            System.out.println(scorelen);
            XSSFWorkbook book = new XSSFWorkbook();
            Sheet sheet = book.createSheet("学生成绩");
            XSSFCellStyle reportNameStyle = book.createCellStyle();
            XSSFFont reportNameFont = book.createFont();
            reportNameFont.setFontName("宋体");
            reportNameFont.setFontHeightInPoints((short) 12);// 字号
            reportNameFont.setBold(true);// 加粗
            reportNameStyle.setFont(reportNameFont);
            reportNameStyle.setAlignment(HorizontalAlignment.CENTER);// 左右居中
            reportNameStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
            reportNameStyle.setWrapText(true);
            reportNameStyle.setBorderBottom(BorderStyle.THIN);
            reportNameStyle.setBorderLeft(BorderStyle.THIN);
            reportNameStyle.setBorderRight(BorderStyle.THIN);
            reportNameStyle.setBorderTop(BorderStyle.THIN);
            Row row = sheet.createRow(0);
            Cell cell=null;
            cell=row.createCell(0);
            cell.setCellValue("ID");
            cell=row.createCell(1);
            cell.setCellValue("姓名");
            for(int i=1;i<=scorelen;i++){
                cell=row.createCell(i+1);
                cell.setCellValue("第"+i+"次作业");
            }
            cell=row.createCell(scorelen+1);
            cell.setCellValue("总分");
            for(int i=0;i<list.size();i++){
                DTO ndto = hashtable.get(list.get(i).getUserid());
                row = sheet.createRow(i+1);
                cell=row.createCell(0);
                cell.setCellValue(list.get(i).getUserid());
                cell=row.createCell(1);
                cell.setCellValue(ndto.getUserName());
                List<String> scoreList=ndto.getList();
                int allscore=0;
                for(int j=0;j<scoreList.size();j++){
                    cell=row.createCell(j+2);
                    cell.setCellValue(scoreList.get(j));
                    allscore = allscore + new Integer(scoreList.get(j));
                }
                cell=row.createCell(scorelen+1);
                cell.setCellValue(String.valueOf(allscore));
            }
            return book;
        }
        return null;
    }
}
